﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UcAsp.Models;
namespace UcAsp.Controllers
{
    public class ContentManagerController : Controller
    {
        //
        // GET: /ContentManager/
        private UcAspNetEntities entities = new UcAspNetEntities();
        public ActionResult Index()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            List<Articles> list = new List<Articles>();
            var query = from c in entities.Articless
                        join m in entities.Categoriess
                        on c.CategorieID equals m.ID
                        where m.ParentID == 2
                        orderby c.ID descending
                        select c;
                        
            int pagesize = 10;
            int pageindex = 0;
            if (Request.QueryString["page"] != null)
            {
                pageindex = int.Parse(Request.QueryString["page"].ToString())-1;
            }
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count()+9)/10;

            list = query.Skip(pageindex * pagesize).ToList<Articles>().Take(pagesize).ToList();




            return View(list);
        }
        public ActionResult CategoriesManger()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            if (Request.QueryString["ID"] == null)
            {
                List<Categories> list = new List<Categories>();
                list = entities.Categoriess.OrderByDescending(o=>o.Rate).Where(o => o.ParentID == 0).ToList();
                return View(list);
            }
            else
            { 
                int id =int.Parse(Request.QueryString["ID"].ToString());
                List<Categories> list = new List<Categories>();
                list = entities.Categoriess.OrderByDescending(o=>o.Rate).Where(o => o.ParentID == id).ToList();
                return View(list);
            }
        }
        public ActionResult EditCategories()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            List<Categories> list = entities.Categoriess.Where(o => o.ParentID == 0).ToList();
            IEnumerable<Categories> accessIDs = list;
            list.Insert(0,(new Categories { ID = 0, CategorieName = "选择分类" }));

            SelectList selectlist = new SelectList(accessIDs, "ID", "CategorieName");

            ViewData["selectlist"] = selectlist;
            Categories categories = new Categories();
            int id = 0;
            if (Request.QueryString["ID"] != null)
            {
                id = int.Parse(Request.QueryString["ID"].ToString());
                categories = entities.Categoriess.FirstOrDefault(o=>o.ID==id);
            }
            return View(categories);
        }
        [ValidateInput(false)]
        public ActionResult SaveCategories(Categories model)
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            
            if (model.ID == 0)
            {
                
                entities.Categoriess.Add(model);
                entities.SaveChanges();
            }
            else
            {
                Categories cate = entities.Categoriess.FirstOrDefault(o => o.ID == model.ID);
                cate.KeyWords = model.KeyWords;
                cate.ParentID = model.ParentID;
                cate.Remark = model.Remark;
                cate.TmepPath = model.TmepPath;
                cate.CategorieName = model.CategorieName;
                cate.Rate = model.Rate;
                entities.SaveChanges();
                
            }
            
            Message message = new Message { Result = true, Messages = "" };
            return Json(message, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ActionManger()
        {
            List<Articles> list = new List<Articles>();
            var query = from c in entities.Articless
                        join m in entities.Categoriess
                        on c.CategorieID equals m.ID
                        where m.ParentID == 7
                        orderby c.ID descending
                        select c;

            int pagesize = 20;
            int pageindex = 0;
            if (Request.QueryString["page"] != null)
            {
                pageindex = int.Parse(Request.QueryString["page"].ToString()) - 1;
            }
            ViewData["PageIndex"] = pageindex + 1;
            ViewData["PageCount"] = (query.Count() + 19) / 20;

            list = query.Skip(pageindex * pagesize).ToList<Articles>().Take(pagesize).ToList();




            return View(list);
        }
        public ActionResult GetContent()
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"] != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }

            int id = 0;
            string types = Request.QueryString["Type"];
            if (Request.QueryString["ID"] != null)
            {
                id = int.Parse(Request.QueryString["ID"].ToString());
            }
            List<Categories> list = entities.Categoriess.Where(o => o.ParentID == 2).ToList();
            IEnumerable<Categories> accessIDs = list;


            SelectList selectlist = new SelectList(accessIDs, "ID", "CategorieName");
            ViewData["selectlist"] = selectlist;

            List<Articles> teachlist = entities.Articless.Where(o => o.CategorieID == 10).ToList();
            IEnumerable<Articles> teachIDs = teachlist;


            SelectList selectteachlist = new SelectList(teachIDs, "ID", "Title");
            ViewData["teach"] = selectteachlist;




            List<Categories> courselist = entities.Categoriess.Where(o => o.ParentID == 11).ToList();
            IEnumerable<Categories> courseIDs = courselist;


            SelectList selectcourselist = new SelectList(courseIDs, "ID", "CategorieName");
            ViewData["course"] = selectcourselist;


            List<Categories> worklist = entities.Categoriess.Where(o => o.ParentID == 21).ToList();
            IEnumerable<Categories> workIDs = worklist;


            SelectList selectworklist = new SelectList(workIDs, "ID", "CategorieName");
            ViewData["work"] = selectworklist;

            List<Categories> actionlist = entities.Categoriess.Where(o => o.ParentID == 7).ToList();
            IEnumerable<Categories> actionIDs = actionlist;


            SelectList actionworklist = new SelectList(actionIDs, "ID", "CategorieName");
            ViewData["action"] = actionworklist;



            Articles articles = new Articles();
            if (id != 0)
            {
                articles = entities.Articless.FirstOrDefault(o=>o.ID==id);
            }
            string themes = "~/Views/ContentManager/GetContent.cshtml";
            switch (types)
            {
                case "Artice":
                    themes = "~/Views/ContentManager/GetContent.cshtml";
                    break;
                case "Action":
                    themes = "~/Views/ContentManager/GetAction.cshtml";
                    break;
                case "Teach":
                    themes = "~/Views/ContentManager/GetTeach.cshtml";
                    break;
                case "TeachWork":
                    themes = "~/Views/ContentManager/GetTeachWork.cshtml";
                    break;
                case "Course":
                    themes = "~/Views/ContentManager/GetCourse.cshtml";
                    break;
                case "Rencai":
                    themes = "~/Views/ContentManager/GetRencai.cshtml";
                    break;
                case "Works":
                    themes = "~/Views/ContentManager/GetWorks.cshtml";
                    break;
                case "Video":
                    themes = "~/Views/ContentManager/GetVideo.cshtml";
                    break;

                case "Ask":
                    themes = "~/Views/ContentManager/GetAsk.cshtml";
                    break;
                case "Jobs":
                    themes = "~/Views/ContentManager/GetJobs.cshtml";
                    break;
                case "other":
                    themes = "~/Views/ContentManager/GetOther.cshtml";
                    break; 
                default:
                    themes = "~/Views/ContentManager/GetAction.cshtml";
                    break;
            }
            return View(themes,articles);
        }
         [ValidateInput(false)]
        public ActionResult SaveContent(Articles model)
        {
            if (Session["Info"] == null)
            {

                return Redirect("/adminlogin/login");
            }
            else
            {
                if (Session["Info"].ToString() != "Admin")
                {
                    return Redirect("/adminlogin/login");
                }
            }
            if (model.ID == 0)
            {
                model.AddDatetime = DateTime.Now;
                entities.Articless.Add(model);
            }
            else
            {
                Articles newmodel = entities.Articless.FirstOrDefault(o => o.ID == model.ID);
                newmodel.Title = model.Title;
                newmodel.ArticleContent = model.ArticleContent;
                newmodel.CategorieID = model.CategorieID;
                newmodel.Description = model.Description;
                if (!string.IsNullOrEmpty(model.IconUrl))
                {
                    newmodel.IconUrl = model.IconUrl;
                }
                newmodel.MetaDescrition = model.MetaDescrition;
                newmodel.MetaKeyword = model.MetaKeyword;
                newmodel.Tag = model.Tag;
                if (!string.IsNullOrEmpty(model.Note1))
                {
                    newmodel.Note1 = model.Note1;
                }
                newmodel.Note2 = model.Note2;
                newmodel.Note3 = model.Note3;
                newmodel.Note4 = model.Note4;
                newmodel.Note5 = model.Note5;
                newmodel.Note6 = model.Note6;
                newmodel.Note7 = model.Note7;
                newmodel.Note8 = model.Note8;
                newmodel.Note9 = model.Note9;
                newmodel.Int1 = model.Int1;
                newmodel.Int2 = model.Int2;
                newmodel.Rate = model.Rate;
                newmodel.Float1 = model.Float1;
               
            }
            entities.SaveChanges();
            Message message = new Message { Result = true, Messages = "" };
            return Json(message,JsonRequestBehavior.AllowGet);
        }
        public ActionResult ShowArticle()
         {
             if (Session["Info"] == null)
             {

                 return Redirect("/adminlogin/login");
             }
             else
             {
                 if (Session["Info"] != "Admin")
                 {
                     return Redirect("/adminlogin/login");
                 }
             }
             int id = int.Parse(Request.Form["ID"]);
             bool release = bool.Parse(Request.Form["Release"]);
             Articles model = entities.Articless.FirstOrDefault(o => o.ID == id);
             model.IsRelease = release == true ? false : true;
             entities.SaveChanges();
             Message message = new Message { Result = true, Messages = "" };
             return Json(message,JsonRequestBehavior.AllowGet);
        }
        public ActionResult Recommend()
        {
            int id = int.Parse(Request.Form["ID"]);
            bool recommend = bool.Parse(Request.Form["Recommend"]);
            Articles model = entities.Articless.FirstOrDefault(o => o.ID == id);
            model.IsRecommend = recommend == true ? false : true;
            entities.SaveChanges();
            Message message = new Message { Result = true, Messages = "" };
            return Json(message, JsonRequestBehavior.AllowGet);
        }
        public ActionResult DelArtice(int id)
        {
            Articles art = entities.Articless.FirstOrDefault(o => o.ID == id);
            entities.Articless.Remove(art);
            entities.SaveChanges();
            Message message = new Message { Result = true, Messages = "" };
            return Json(message, JsonRequestBehavior.AllowGet);
        }
    }
}
